import 'package:dio/dio.dart';
import '../generated/json/base/json_convert_content.dart';
import 'http_response.dart';
import 'http_transformer.dart';

class DefaultHttpTransformer extends HttpTransformer {
  @override
  HttpResponse<T> parse<T>(Response response) {
    if (response.data['Response']['Header']['RESULT'] == "SUCCESS") {
      Map result = response.data['Response'];
      if(result.containsKey('Body')) {
        if(response.data["Response"]["Body"].toString().isEmpty) {
          return HttpResponse.success(null);
        }else{
          return HttpResponse.success(JsonConvert.fromJsonAsT<T>(response.data["Response"]["Body"]));
        }
      }else {
        return HttpResponse.success(null);
      }
    } else {
      Map result = response.data['Response']["Header"];
      if(result.containsKey('RESULTMESSAGE')) {
        return HttpResponse.failure(
            errorMsg: response.data["Response"]["Header"]["RESULTMESSAGE"],
            errorCode: response.data["Response"]["Header"]["RESULTMESSAGE"]);
      }else{
        return HttpResponse.failure(
            errorMsg: '请求失败', errorCode: '500');
      }
    }
  }

  static DefaultHttpTransformer _instance = DefaultHttpTransformer._internal();

  DefaultHttpTransformer._internal();

  factory DefaultHttpTransformer.getInstance() => _instance;
}
